﻿using System;
using Guru.Data;
using System.Data;

public partial class FE_CheckInModal : HSMPage
{
    protected int BookingID
    {
        get
        {
            if (string.IsNullOrEmpty(SecureQueryString["InvoiceID"]))
                return 0;
            return int.Parse(SecureQueryString["InvoiceID"]);
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string mode = Request.QueryString["mode"].ToSafeString().Trim();
            if (mode == "2")  // Make a new booking     
            {
                this.uxCheckIn.Visible = false;
                this.uxCancelBook.Visible = false;
            }
            else if (this.BookingID <= 0) // Check in without booking
            {                
                this.uxCancelBook.Visible = false;
                this.uxSaveBook.Visible = false;
            }
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.PageType = GuruPageType.Modal;
        this.FixedModalWidth = HSMConstants.DefaultFixedModalWidth;
        if (!IsPostBack)
        {
            if (this.BookingID > 0)
            {
                DataTable dt = this.GetBookingDetails(this.BookingID);
                uxCustomerDetails.GuestID = int.Parse(dt.Rows[0]["GuestID"].ToString()); ;
                uxCustomerDetails.LoadGuest();
                uxCheckInDetails.BindBookingDetails(dt);
            }
            else if (IsSecureQueryString && SecureQueryString["GuestID"].IsNotEmpty())
            {
                uxCustomerDetails.GuestID = int.Parse(SecureQueryString["GuestID"]);
                uxCustomerDetails.LoadGuest();
            }
        }
    }
    protected void uxCheckIn_Click(object sender, EventArgs e)
    {
        if (this.uxCheckInDetails.RoomList.Count <= 0)
        {
            this.AlertClient(Resources.MessageManager.RequireSelectOneRoom);
            return;
        }
        int guestID = this.uxCustomerDetails.Save();
        this.uxCheckInDetails.Save(HSMEnums.RoomAction.CheckIn, guestID);
        this.CloseModal(1, "parent.ReloadPage();");
    }
    protected void uxSaveBook_Click(object sender, EventArgs e)
    {
        HSMEnums.RoomAction action = (this.BookingID > 0) ? action = HSMEnums.RoomAction.UpdateBook : HSMEnums.RoomAction.Book;
        int guestID = this.uxCustomerDetails.Save();
        this.uxCheckInDetails.Save(action, guestID);
        this.CloseModal(1, "parent.ReloadPage();");
    }
    protected void uxCancelBook_Click(object sender, EventArgs e)
    {
        this.uxCheckInDetails.CancelBook(this.BookingID);
        this.CloseModal(1, "parent.ReloadPage();");
    }

    protected DataTable GetBookingDetails(int bookingID)
    {
        GuruParameterCollection parameters = new GuruParameterCollection();      
        parameters.AddUserLoggedParams();
        parameters.Add("@BookingID", bookingID, DbType.Int32);
        return HSMCommon.HSMBusiness.GetDataTable("spa_HSM_Booking_GetDetails", parameters);        
    }
}
